$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        dom:null,
        init:function () {
            this.dom = $('.interviewer').eq(0).clone();
            $('[name=iInterviewer]').select2();
            this.listener();
            this.funObj().init();
        },
        listener:function () {
            var $this = this;
            //新增面试负责人
            $('#addInterviewer').click(function () {
                $this.funObj().addInterviewer($(this));
            });

            $('#bt_modal_confirm', parent.document).click(function () {
                $this.funObj().submitAction();
            })

        },
        funObj:function () {
            var $this = this;
            return {
                addInterviewer:function (target) {
                    var length = $('.interviewer').length +1;
                    var dom = $this.dom.clone();
                    dom = $(dom);
                    dom.find('td:first').html('面试('+length+')');
                    dom.find('[name=iIndex]').val(length);
                    dom.find('[name=iInterviewer]').select2();
                    target.parent().parent().before(dom);
                },
                init:function () {
                    if (maxIndex > 3) {
                        for (var i = maxIndex -3 ; i > 0 ;i--) {
                            $('#addInterviewer').click();
                        }
                    }
                    $.each(info,function (i,item){
                        var index = item['iIndex'];
                        $this.funObj().fillData($('.interviewer').eq(index-1), item);
                    });
                },
                fillData:function(dom,row) {
                    dom.find('[name=iHIId]').val(row['iHIId']);
                    dom.find('[name=iInterviewer]').val(row['iInterviewer']).trigger('change');
                },
                collectData:function () {
                    var arr = [];
                    $('.interviewer').each(function () {
                        var obj = {};
                        obj.iHIId = $(this).find('[name=iHIId]').val();
                        obj.iIndex = $(this).find('[name=iIndex]').val();
                        obj.iSourceId = $(this).find('[name=iSourceId]').val();
                        obj.iInterviewer = $(this).find('[name=iInterviewer]').val();
                        if (obj.iInterviewer) {
                            arr.push(obj);
                        }
                    });
                    return arr;
                },
                submitAction:function () {
                    var obj = {};
                        obj.interviewArr = $this.funObj().collectData();
                        xhExtra.ajaxRequest('hrm/resource/saveInterviewer',obj,{},function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            window.top.xhTop.closeModal();
                        });
                }
            }
        }
    }
};